package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.controller.dto.RoleDto;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.RoleMapper;
import com.itheima.mapper.RoleMenuMapper;
import com.itheima.pojo.Role;
import com.itheima.pojo.RoleMenu;
import com.itheima.service.RoleService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 角色服务接口实现类
 * @Author makun
 * @Date 2023/9/27 10:36
 * @Version 1.0
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public Page<Role> findPage(QueryPageBean queryPageBean) {
        // 查询条件
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if (!StringUtils.isNullOrEmpty(queryString)) {
            roleLambdaQueryWrapper.like(Role::getName, queryString)
                    .or()
                    .like(Role::getKeyword, queryString);
        }
        // 分页条件
        Page<Role> rolePage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 执行查询
        baseMapper.selectPage(rolePage, roleLambdaQueryWrapper);
        return rolePage;
    }

    @Override
    public List<Integer> getMenuIdsByRoleId(Integer roleId) {
        return roleMenuMapper.selectList(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, roleId)).stream()
                .map(RoleMenu::getMenuId)
                .collect(Collectors.toList());
    }

    @Override
    public boolean edit(Role role, Integer[] menuIdsOfRole) {
        Integer roleId = role.getId();
        // 清空菜单与角色的关系
        roleMenuMapper.delete(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, roleId));
        // 增加菜单与角色的关系
        for (Integer menuId : menuIdsOfRole) {
            roleMenuMapper.insert(new RoleMenu(roleId, menuId));
        }
        // 修改角色的基础信息
        return baseMapper.updateById(role) == 1;
    }

    @Override
    public boolean save(Role role, Integer[] menuIdsOfRole) {
        // 添加角色基础信息
        boolean flag = baseMapper.insert(role) == 1;
        // 添加角色与菜单的关系
        for (Integer menuId : menuIdsOfRole) {
            roleMenuMapper.insert(new RoleMenu(role.getId(), menuId));
        }
        return flag;
    }

    @Override
    public boolean saveOne(RoleDto roleDto) {
        // 添加角色基础信息
        boolean flag = baseMapper.insert(roleDto) == 1;
        // 添加角色与菜单的关系
        for (Integer menuId : roleDto.getMenuIds()) {
            roleMenuMapper.insert(new RoleMenu(roleDto.getId(), menuId));
        }
        return flag;
    }

    @Override
    public boolean editOne(RoleDto roleDto) {
        Integer roleId = roleDto.getId();
        // 清空菜单与角色的关系
        roleMenuMapper.delete(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, roleId));
        // 增加菜单与角色的关系
        for (Integer menuId : roleDto.getMenuIds()) {
            roleMenuMapper.insert(new RoleMenu(roleId, menuId));
        }
        // 修改角色的基础信息
        return baseMapper.updateById(roleDto) == 1;
    }

    @Override
    public List<Role> getAll(String queryString) {
        // 查询条件
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(queryString)) {
            roleLambdaQueryWrapper.like(Role::getName, queryString)
                    .or()
                    .like(Role::getKeyword, queryString);
        }
        return baseMapper.selectList(roleLambdaQueryWrapper);
    }
}
